package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessFarmSystem;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.GoodsEvaluateVO;
import com.digitalchina.lxnsy.entity.MyOrderListVO;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hedgehog.ratingbar.RatingBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/8/9 23:02
 * 修改备注：
 * ================================================
 */
public class EvaluateOrderActivity extends BaseActivity {

    @Bind(R.id.rating_bar_describe)
    RatingBar ratingBarDescribe;
    @Bind(R.id.rating_bar_service)
    RatingBar ratingBarService;
    @Bind(R.id.evaluation_product_item_layout)
    LinearLayout evaluationProductItemLayout;
    @Bind(R.id.evaluate_ck_anonymous)
    CheckBox evaluateCkAnonymous;
    @Bind(R.id.evaluate_btn_publish)
    TextView evaluateBtnPublish;
    @Bind(R.id.layout)
    RelativeLayout layout;

    private MyOrderListVO myOrderListVO;
    private List<EditText> edtEvaList = new ArrayList<>();
    private int ratingCountArray[];

    private int accordStore = 0; // 符合度评分
    private int serviceScore = 0; // 服务态度评分
    private String isAnonymity = "1"; // 是否匿名 1 是 0否

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_evaluate_order);
        setTitle("订单评价");
    }

    @Override
    public void initMember() {

        layout.setFocusable(true);
        layout.setFocusableInTouchMode(true);

        myOrderListVO = (MyOrderListVO) getIntent().getSerializableExtra("myOrderListVO");

        if (myOrderListVO == null)
            return;

        ratingCountArray = new int[myOrderListVO.getOrderItems().size()];

        for (int i = 0; i < myOrderListVO.getOrderItems().size(); i++) {

            ratingCountArray[i] = 0;

            View view = LayoutInflater.from(this).inflate(R.layout.item_evaluate_goods, null);

            SimpleDraweeView sdv = (SimpleDraweeView) view.findViewById(R.id.item_evaluation_goods_image);
            TextView tvName = (TextView) view.findViewById(R.id.item_evaluation_goods_name);
            TextView tvType = (TextView) view.findViewById(R.id.item_evaluation_goods_type);
            EditText edtEva = (EditText) view.findViewById(R.id.item_evaluation_content);
            RatingBar ratingBar = (RatingBar) view.findViewById(R.id.item_evaluation_rating_bar);

            FrescoUtil.showImageSmall(myOrderListVO.getOrderItems().get(i).getProductImg(), sdv);
            tvName.setText(myOrderListVO.getOrderItems().get(i).getProductName());
            tvType.setText(myOrderListVO.getOrderItems().get(i).getCommodityType());

            final int finalI = i;
            ratingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
                @Override
                public void onRatingChange(float RatingCount) {
                    ratingCountArray[finalI] = (int) RatingCount;
                }
            });
            edtEvaList.add(edtEva);


            evaluationProductItemLayout.addView(view);

        }

    }

    @Override
    public void addListener() {

        ratingBarDescribe.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float RatingCount) {
                accordStore = (int) RatingCount;
            }
        });

        ratingBarService.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float RatingCount) {
                serviceScore = (int) RatingCount;
            }
        });

        evaluateCkAnonymous.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    isAnonymity = "1";
                else
                    isAnonymity = "0";
                LogUtils.e(isAnonymity);
            }
        });

        evaluateBtnPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (accordStore == 0) {
                    showToast("请对商品描述符合度打分");
                    return;
                }
                if (serviceScore == 0) {
                    showToast("请对商户的服务态度打分");
                    return;
                }

                List<GoodsEvaluateVO> evaluateList = new ArrayList<>();// 商品评价

                if (myOrderListVO == null)
                    return;

                for (int i = 0; i < myOrderListVO.getOrderItems().size(); i++) {
                    if (StringUtil.isEditEmpty(edtEvaList.get(i)) || ratingCountArray[i] == 0) {
                        showToast("请给商品做出评价或评分");
                        return;
                    }
                    GoodsEvaluateVO goodsEvaluateVO = new GoodsEvaluateVO();
                    goodsEvaluateVO.setProductId(myOrderListVO.getOrderItems().get(i).getProductId());
                    goodsEvaluateVO.setContent(edtEvaList.get(i).getText().toString());
                    goodsEvaluateVO.setScore(String.valueOf(ratingCountArray[i]));
                    goodsEvaluateVO.setAtrrName(myOrderListVO.getOrderItems().get(i).getAttrName());
                    evaluateList.add(goodsEvaluateVO);
                }

                String evaluateProductStr = JSON.toJSONString(evaluateList, true);

                BusinessFarmSystem.mobileEvaluateOrder(EvaluateOrderActivity.this, myOrderListVO.getOrderNo(), String.valueOf(accordStore), String.valueOf(serviceScore), isAnonymity, evaluateProductStr, mHandler);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_MOBILE_EVALUATE_ORDER_SUCCESS:
                        showToast("评价成功");
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case MSG.MSG_MOBILE_EVALUATE_ORDER_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
